import json
from frame.bases.base_common import BaseCommon
from frame.utils.log_utils import logger


class BaseUser(BaseCommon):
    '''
    定义user基类
    '''

    __BaseUserUrl = "https://litemall.hogwarts.ceshiren.com/wx/"

    def __init__(self):
        data = {
            "username": "user123",
            "password": "user123"
        }
        req = {
            "url": self.__BaseUserUrl + "auth/login",
            "method": "post",
            "json": data
        }
        result = self.do_send(req)
        self.token = result.json().get('data').get('token')

    def send_api(self, req):
        if req.get('headers'):
            req['headers']['X-Litemall-Token'] = self.token
        else:
            req['headers'] = {"X-Litemall-Token": self.token}
        url = req['url']
        req['url'] = self.__BaseUserUrl + url

        r = self.do_send(req)
        logger.info(f"{url}接口的响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")

        return r
